import com.mysql.cj.jdbc.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCDemo3 {
    public static void main(String[] args) throws SQLException {
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("354684");

        Connection connection = mysqlDataSource.getConnection();

        String sql ="select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet i = statement.executeQuery();
        while (i.next()){
            //获取到这一行的学号列
            int id = i.getInt("id");
            //获取到这一行的姓名列
            String name = i.getString("name");
            System.out.println("id:"+id+",name:"+name);
        }



        i.close();
        connection.close();
        statement.close();

    }
}
